﻿using System;
using System.Text;
using System.Web.DynamicData;
using System.Web.UI;

namespace NotAClue.ComponentModel.DataAnnotations
{
    // attribute to determine what mode the FileImage_Edit is in
    // the default is Select.
    [AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
    public sealed class ImageUploadAttribute : Attribute
    {
        /// <summary>
        /// Gets or sets the editor type.
        /// </summary>
        /// <value>The editor type.</value>
        public EditorType Editor { get; set; }

        /// <summary>
        /// Gets or sets the file upload path.
        /// </summary>
        /// <value>The file upload path.</value>
        public String FileUploadPath { get; set; }

        public ImageUploadAttribute(EditorType editorType, String fileUploadPath) 
        { 
            Editor = editorType;
            FileUploadPath = fileUploadPath;
        }

        private String[] imageTypes;
        /// <summary>
        /// File types allowed to be upload
        /// </summary>
        public String[] FileTypes 
        {
            get
            {
                if (imageTypes != null && imageTypes.Length > 0)
                    return imageTypes;
                else
                    return DefaultExtensions;
            }
            set { imageTypes = value; }
        }

        private static String[] DefaultExtensions= new String[] { "gif", "png", "jpeg", "jpg" };

        public String FileTypeString()
        {
            var stringFileTypes = new StringBuilder();
            foreach (var s in FileTypes)
                stringFileTypes.Append(s + ",");

            return stringFileTypes.ToString().Substring(0, stringFileTypes.Length - 1); ;
        }
    }

    //public static class FileImageHelper
    //{
    //    public static void UpdateHeightWidth(ref int scaleWidth, ref int scaleHeight, String fileName)
    //    {
    //        System.Drawing.Image image = System.Drawing.Image.FromFile(fileName);
    //        float originalHeight = image.Height;
    //        float originalWidth = image.Width;
    //        float height = 0;
    //        float width = 0;
    //        if (scaleWidth > 0 && scaleHeight == 0)
    //        {
    //            //scale to width only
    //            //calculate height based on width keeping correct proportions
    //            height = originalHeight * (scaleWidth / originalWidth);
    //            scaleHeight = (int)height;
    //        }
    //        else if (scaleHeight > 0 && scaleWidth == 0)
    //        {
    //            //scale to height only
    //            //calculate height based on width keeping correct proportions
    //            width = originalWidth * (scaleHeight / originalHeight);
    //            scaleWidth = (int)width;
    //        }
    //        //else scale to both dims sent
    //    }
    //}
}